package com.dlc.felear.xiaoensale.customView;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.utils.InputMethodManagerUtil;


public class TextInputDialog extends Dialog implements View.OnClickListener {
    private Button btnConfirm;
    private TextView tvTitle;
    private EditText editText1;
    private Context context;
    private AppCompatActivity activity;
    private TextInputDialogCallback callback;
    private int id;

    public TextInputDialog(@NonNull Context context, TextInputDialogCallback callback) {
        super(context);
        this.callback = callback;
        initView(context);
    }

    public TextInputDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        initView(context);
    }


    private void initView(Context context) {
        this.context = context;
        this.activity = (AppCompatActivity)context;
        View root = LayoutInflater.from(context).inflate(R.layout.text_input_dialog, null);
        tvTitle = root.findViewById(R.id.tv_title);
        btnConfirm = root.findViewById(R.id.btn_confirm);
        editText1 = root.findViewById(R.id.et_value_1);
        btnConfirm.setOnClickListener(this);
        setContentView(root);
        setCanceledOnTouchOutside(false);
    }


    public void showDialog(int id) {
        this.id=id;
        InputMethodManagerUtil.inputMethodStatus(activity,editText1,true);
        show();
    }
    public void setEditText1(String info){
        editText1.setText(info);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_confirm:
                callback.textInputResult(editText1.getText().toString().trim(),id);
                clearData();
                dismiss();
                break;
            default:
        }
    }

    private void clearData(){
        InputMethodManagerUtil.inputMethodStatus(activity,editText1,false);
        editText1.setText("");
        editText1.setHint("");
    }

    public void setTitle(String tips){
        tvTitle.setText(tips);
    }




    public interface TextInputDialogCallback {
        void textInputResult(String value, int id);
    }
}
